package everyday;

/**
 * @ClassName D1721
 * @Description https://leetcode-cn.com/problems/volume-of-histogram-lcci/
 * @Author Yin
 * @Date 2021/4/2 10:18
 * @Version 1.0
 */
public class D1721 {
    public static int trap(int[] height) {
        int n = height.length;
        if (n == 0) {
            return 0;
        }
        // 从左往右遍历
        int[] leftMax = new int[n];
        leftMax[0] = height[0];
        for (int i = 1; i < n; ++i) {
            leftMax[i] = Math.max(leftMax[i - 1], height[i]);
        }
        // 从右往左遍历
        int[] rightMax = new int[n];
        rightMax[n - 1] = height[n - 1];
        for (int i = n - 2; i >= 0; --i) {
            rightMax[i] = Math.max(rightMax[i + 1], height[i]);
        }
        int ans = 0;
        // 获取重复的部分
        for (int i = 0; i < n; ++i) {
            ans += Math.min(leftMax[i], rightMax[i]) - height[i];
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(trap(new int[]{2,8,5,5,6,1,7,4,5}));
    }
}
